#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the version 3 of the GNU Lesser General Public License
#   as published by the Free Software Foundation.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Lesser General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# Copyright (c) NEC Deutschland GmbH, NEC HPC Europe
#
# $Id$


from aggmon.databases.metric.time_series_database import TimeSeriesDatabase
from basic_types import LogRecord


__all__ = ["LOG"]


class LOG(TimeSeriesDatabase):
    """
    A LOG contains the history of string valued metrics, and should just be built
    of the state changes, not every measured state.
    """

    def append( self, time_ns, value, output="" ):
        """
        Append a value (and output string) to the current LOG database.
        @param time_ns: time in 10E-9 seconds since epoch
        @param value: the actual value of the metric
        @param output: a possibly available output string
        @return: nothing
        """
        self.append_record( LogRecord( time_ns, value, output=output ) )

    def filter_records( self, records ):
        """
        Filter records list. Eliminate duplicates such that one can only see the state changes.
        """
        new_records = []
        last_record = None
        for record in records:
            if last_record is None:
                last_record = record
                new_records.append( record )
                continue
            if record.value == last_record.value:
                if hasattr( record, "output" ):
                    if record.output == last_record.output:
                        continue
                else:
                    continue
            new_records.append( record )
            last_record = record
        return new_records

    def insert( self, time_ns, value, output="" ):
        """
        Inserts a time, value and output string into the database.
        @param time_ns: time in 10E-9 seconds since epoch
        @param value: the actual value of the metric
        @param output: log output (string type)
        @return: nothing
        """
        self.insert_record( LogRecord( time_ns, value, output=output ) )

    def record( self ):
        return LogRecord()


from basic_types.factory import Factory
Factory.set_factory( "LOG", LOG, __name__ )
